//logs.js
const app = getApp()
var module=require("../../../utils/jinbo/canzhanshang.js");
var lang=app.globalData.lang;
Page({
  data: {
    companies:null,
  	language:app.globalData.lang,
  	// choose:module[lang].choose,
  	current:'',
  	currentChoose:null,
    showRadio:false,
    showCheckbox:false,
  	chooseItmes:null,
    inputShowed: false,
    intercontinentalSelected: false,
    intercontinentalShowed: false,
    intercontinental: null,
    countrySelected: false,
    countryShowed: false,
    country: null,
    specialSelected: false,
    specialShowed: false,
    special: null,
    typeSelected: false,
    typeShowed: false,
    type: null,
    inputVal: "",
    tabId: 0,
    businessScope: null,
    specialTitle:'',
    typeTitle:'',
	page:0,
  },
  onLoad: function() {
    var lang=app.globalData.lang;
    this.setData({
      language:app.globalData.lang,
      currentChoose:module[lang].choose
    });
    this.changeCompanies(true,true);
  },
  checkDetail:function(e){
    wx.navigateTo({
      url:'/pages/jinbo/canzhanshangxiangqing/index?id='+e.currentTarget.dataset.id
    });
  },
  clearInput: function() {
    this.setData({
      inputVal: ""
    });
    this.changeCompanies(true,true);
  },
  hideSelect: function (e) {
    this.setData({
      intercontinentalSelected: false,
      countrySelected: false,
      specialSelected: false,
      typeSelected: false,
      showRadio: false,
      showCheckbox: false
    });
  },
  inputTyping: function(e) {
    this.setData({
      inputVal: e.detail.value
    });
    this.hideSelect();
    this.changeCompanies(false,true);
  },
  submitFn(e) {
    let saveCheckAttentionData=[];
    let j=0;
    for (let i=0;i< this.data.special.length;i++) {
      if(this.data.special[i].selected)
        saveCheckAttentionData[j++]=this.data.special[i].id;
    }
    this.data.currentChoose.special=saveCheckAttentionData;
    let saveCheckTypeData=[];
    j=0;
    for (let i=0;i< this.data.type.length;i++) {
      if(this.data.type[i].selected)
        saveCheckTypeData[j++]=this.data.type[i].id;
    }
    this.data.currentChoose.type=saveCheckTypeData;
    this.setData({
    currentChoose:this.data.currentChoose
    });
    this.changeCompanies(false,true);
  },
  resetFn(e) {
    if(this.data.current=='special'){
      for (let i=0;i< this.data.special.length;i++) {
        this.data.special[i].selected=false;
      }
      this.setData({
        special:this.data.special,
        specialTitle:'',
        typeTitle:'',
      });
    }else{
      for (let i=0;i< this.data.type.length;i++) {
        this.data.type[i].selected=false;
      }
      this.setData({
        type:this.data.type,
        specialTitle:'',
        typeTitle:'',
      });
    }
  },
 
  chooseCheckboxItem:function(e){
    const index=e.currentTarget.dataset.index;
    if(this.data.current=='special'){
      this.data.special[index].selected=!this.data.special[index].selected;
      this.data.specialTitle+=this.data.specialTitle==''?this.data.special[index].title:','+this.data.special[index].title;
      this.setData({
        specialTitle:this.data.specialTitle,
        special:this.data.special,
      });
    }else{
      this.data.type[index].selected=!this.data.type[index].selected; 
      this.data.typeTitle+=this.data.typeTitle==''?this.data.type[index].title:','+this.data.type[index].title;
      this.setData({
        typeTitle:this.data.typeTitle,
        type:this.data.type,
      });
    }
  },
  chooseRadioItem:function(e){
    const index=e.currentTarget.dataset.index;
    if(this.data.current=='intercontinental'){
      this.data.currentChoose.intercontinental.title=this.data.intercontinental[Number(index)].title;
      this.data.currentChoose.intercontinental.id=this.data.intercontinental[Number(index)].id;    
      this.data.currentChoose.country=module[this.data.language].choose.country;
    }else{
      this.data.currentChoose.country.title=this.data.country[Number(index)].title;
      this.data.currentChoose.country.id=this.data.country[Number(index)].id;
    }
    this.setData({
    	currentChoose: this.data.currentChoose,
      intercontinentalSelected:false,
    	countrySelected: false,
    	specialSelected: false,
    	typeSelected: false,
    	showRadio:false,
      showCheckbox:false });
    this.changeCompanies(false,true);
  },
  //点击选择展区，修改三角形方形，修改浮窗的显示情况
  chooseIntercontinental: function(e) {
    this.setData({
    	current:'intercontinental',
    	intercontinentalSelected:!this.data.intercontinentalSelected,
    	countrySelected: false,
    	specialSelected: false,
    	typeSelected: false,
    	showRadio:this.data.intercontinentalSelected?false:true,
      showCheckbox:false
    });
  },
  chooseCountry: function(e) {
    this.setData({
    	current:'country',
    	intercontinentalSelected:false,
    	countrySelected: !this.data.countrySelected,
    	specialSelected: false,
    	typeSelected: false,
    	showRadio:this.data.countrySelected?false:true,
      showCheckbox:false
    });
  },
  chooseSpecial: function(e) {
    this.setData({
    	current:'special',
    	intercontinentalSelected:false,
    	countrySelected: false,
    	specialSelected: !this.data.specialSelected,
    	typeSelected: false,
    	showCheckbox:this.data.specialSelected?false:true,
      showRadio:false
    });
  },
  chooseType: function(e) {
    this.setData({
    	current:'type',
    	intercontinentalSelected:false,
    	countrySelected: false,
    	specialSelected: false,
    	typeSelected: !this.data.typeSelected,
    	showCheckbox:this.data.typeSelected?false:true,
      showRadio:false
    });
  },
  reset:function(){
    for (let i=0;i< this.data.special.length;i++) {
        this.data.special[i].selected=false;
    }
    for (let i=0;i< this.data.type.length;i++) {
        this.data.type[i].selected=false;
    }
      this.setData({
        special:this.data.special,
        type:this.data.type,
        specialTitle:'',
        typeTitle:'',
      });
  },

  selectedTap: function(e) {
    const index=e.currentTarget.dataset.index;
    this.data.currentChoose=module[this.data.language].choose;
    this.data.currentChoose.businessScope.id=this.data.businessScope[Number(index)].id;
    this.data.currentChoose.businessScope.title=this.data.businessScope[Number(index)].title;
    this.setData({
      tabId: index,
      currentChoose:this.data.currentChoose,
    });
    this.reset();
    this.hideSelect();
    this.changeCompanies(false,true);
  },
  appendCompanies:function(){
    this.changeCompanies(false,false);
  },
  changeCompanies:function(isFirst,isRefresh){
    var that=this;
    wx.showLoading({
      title:'正在加载...'
    });
    wx.request({
      url:app.globalData.url+'seller/showList',
      method:'POST',
      data:{
        businessScope:this.data.currentChoose.businessScope.id,
        intercontinental:this.data.currentChoose.intercontinental.id,
        country:this.data.currentChoose.country.id,
        special:this.data.currentChoose.special,
        type:this.data.currentChoose.type,
        language:this.data.language,
        title:this.data.inputVal,
        page:isRefresh?0:this.data.page+1,
        },
      success:function(res){
		if(res.data.code==1){
			console.log('companies');
			if(isRefresh){
				console.log('companies');
			  var companies=res.data.data.companies;
			  var page=0;
			}else{
			  var companies=that.data.companies;
			  var page=that.data.page+1;
			  for(var i=0;i<res.data.data.companies.length;i++){
			  companies.push(res.data.data.companies[i]);
			  }
		}
		console.log('setData');
			that.setData({
			  companies:companies,
			  page:page,
			  intercontinental: res.data.data.intercontinental,
			  country: res.data.data.country,
			  special: isFirst?res.data.data.special:that.data.special,
			  type: isFirst?res.data.data.type:that.data.type,
			  businessScope: res.data.data.businessScope,
			},function(){
				console.log('callback');
			  wx.hideLoading();
			});	
		}else if(res.data.code==0){
			wx.showToast({
            title:res.data.msg,
            icon:"none"
          });
		}
        
      }
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: app.globalData.shareTitle,
      path: app.globalData.sharePath,
      imageUrl: '../../../images/share_img.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
})